//
//  SettingViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/8/30.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "SettingSwitchTableViewCell.h"
#import "CFMacro.h"
#import "LBClearCacheTool.h"
#import "SVProgressHUD.h"

#define kSettingCellReuse @"settingCellReuse"
#define kSettingSwitchTableViewCell @"SettingSwitchTableViewCell"

@interface SettingViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray<NSArray *> *cells;
@end

@implementation SettingViewController

#pragma mark - SettingViewController send noti
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hidePlayerView"
                                                        object:@"playerView"
                                                      userInfo:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showPlayerView"
                                                        object:@"playerView"
                                                      userInfo:nil];
}
#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *array0 = @[@"定时功能", @"预约"];
    NSArray *array1 = @[@"", @"音质流量模式", @"一键清除缓存"];
    NSArray *array2 = @[@"意见反馈"];
    NSArray *array3 = @[@"精品应用", @"关于酷FM", @"版权声明"];
    _cells = @[array0, array1, array2, array3];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:kSettingCellReuse];
    kTalbleViewRegisterNib(@"SettingSwitchTableViewCell", kSettingSwitchTableViewCell);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _cells.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cells[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 0) {
        SettingSwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettingSwitchTableViewCell];
                return cell;
    } else if (indexPath.section == 1 && indexPath.row == 2) {
        SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettingCellReuse];
        NSString *fileSize = [LBClearCacheTool getCacheSizeWithFilePath:kCachesDirectory];
        if ([fileSize integerValue] == 0) {
            cell.textLabel.text = @"一键清除缓存";
        }else {
            cell.textLabel.text = [NSString stringWithFormat:@"一键清除缓存(%@)",fileSize];
        }
        return cell;
    } else {
        SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettingCellReuse];
        cell.titleLabel.text = _cells[indexPath.section][indexPath.row];
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 2) {
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"确定清除缓存吗?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        //创建一个取消和一个确定按钮
        UIAlertAction *actionCancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        //因为需要点击确定按钮后改变文字的值，所以需要在确定按钮这个block里面进行相应的操作
        UIAlertAction *actionOk=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            //清楚缓存
            BOOL isSuccess = [LBClearCacheTool clearCacheWithFilePath:kCachesDirectory];
            if (isSuccess) {
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD showSuccessWithStatus:@"清除成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
            }
        }];
        //将取消和确定按钮添加进弹框控制器
        [alert addAction:actionCancle];
        [alert addAction:actionOk];
        //添加一个文本框到弹框控制器
        //显示弹框控制器
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
